import React from "react";
import { Modal } from "antd";
import { getHomepageData } from "./ServeApi";
import GridCard from "./compontents/grid_card";
import Overview from "./compontents/Overview";
import EnergyTrend from "./compontents/EnergyTrend";
import { AjaxData } from "../../../data/interface/publicInterface";
import "./index.less";
// 数据表头
export interface HomePageProps {}
export interface GridCardDataSouceClass {
  eleNum: number;
  waterNum: number;
  terminalNum: number;
}
export interface HomePageState {
  EnergyTrendDataSouce: any;
  OverviewDataSouce: any;
  GridCardDataSouce: GridCardDataSouceClass;
  loading: boolean;
}
class HomePage extends React.Component<HomePageProps, HomePageState> {
  state = {
    EnergyTrendDataSouce: [],
    OverviewDataSouce: [],
    loading: false,
    GridCardDataSouce: { eleNum: 0, waterNum: 0, terminalNum: 0 },
  };
  render() {
    return (
      <div className="HomePage">
        <GridCard dataSouce={this.state.GridCardDataSouce} />
        <Overview dataSouce={this.state.OverviewDataSouce} />
        <EnergyTrend dataSouce={this.state.EnergyTrendDataSouce} />
      </div>
    );
  }
  componentWillMount() {
    this.getHomeData();
    // getHomepageData();
  }
  /**请求数据 */
  async getHomeData(pream?: any) {
    let _this = this;
    try {
      _this.setState({ loading: true });
      let res: any = await getHomepageData(JSON.stringify(pream)).catch(
        (err) => {
          _this.setState({ loading: false });
        }
      );
      let json: AjaxData = await res;
      if (json.success) {
        _this.setState({
          EnergyTrendDataSouce: json.data,
          OverviewDataSouce: json.data,
          GridCardDataSouce: {
            eleNum: json.data.eleNum,
            waterNum: json.data.waterNum,
            terminalNum: json.data.terminalNum,
          },
        });

        _this.forceUpdate();
      } else {
        Modal.error({
          title: json.message,
          onOk: () => {
            _this.setState({ loading: false });
          },
        });
      }
    } catch (error) {
      Modal.error({
        title: "初始化程序失败，无法读取数据，请刷新页面。",
        onOk: () => {
          _this.setState({ loading: false });
        },
      });
      console.error(error);
    }
  }
}

export default HomePage;
